package com.cheney.weiboo.test.view;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.cheney.weiboo.R;
import com.cheney.weiboo.db.DBHelper;
import com.cheney.weiboo.domain.Users;
import com.cheney.weiboo.utils.DBFactory;
//用户测试SQLite数据库
public class DBTestActivity extends Activity {
	SQLiteDatabase db ;
	DBHelper sh;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_db);
		String dbName = "weiboo.db";
		sh = DBFactory.getInstance(getApplicationContext(), dbName);
		Button insertButton = (Button)findViewById(R.id.insert);
		Button updateButton = (Button)findViewById(R.id.update);
		Button deleteButton = (Button)findViewById(R.id.delete);
		Button picButton = (Button)findViewById(R.id.pic);
		
		String sql = "CREATE TABLE IF NOT EXISTS USERS ("
		 +Users.ID+" varchar primary key,"
		 +Users.TOKEN+" varchar,"
		 +Users.TOKENSECRET+" varchar,"
		 +Users.USERID+" varchar,"
		 +Users.USERNAME+" varchar,"
		 +Users.USERICON+" blob"
		 +")" ;
		db = sh.getWritableDatabase();
		db.execSQL(sql);
		db.close();
		insertButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				ContentValues cv = new ContentValues();
				cv.put("id", Math.random());
				cv.put("userId","userId123");
				cv.put("token", "token1");
				cv.put("tokenSecret", "tokenSecret1");
				cv.put("userName", "userName1");
				cv.put("userIcon",R.drawable.icon);
				db = sh.getWritableDatabase();
				long num = db.insert("users",null, cv);
				System.out.println("插入"+num);
			}
		});
		
		updateButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				db = sh.getWritableDatabase();
				String sql = "update users set userId = 'user1234',token='ttttt' where id = '1'";
				db.execSQL(sql);
				db.close();
				System.out.println("更新");
			}
		});
		
		deleteButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				db = sh.getWritableDatabase();
				db.beginTransaction();
				long num = db.delete("users", "id=?",new String[]{"1"});
				db.endTransaction();
				db.close();
				System.out.println("删除"+num);
			}
		});
	}
}
